﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace proba
{
    public partial class Pogadjanje : Form
    {
        
        
       public List<Button> listaButtona = new List<Button>();
       public ButtonIterator n = new ButtonIterator();
        //public PitanjeIterator n = new PitanjeIterator();

       public PostavkaPitanja f; 
       public TextBox textBox1;
       public TextBox textBox_nick;
       public Button button2;
       public Button button3;
       public Button b;
        //za odbrojavanje
       public int minutesToWait = 4;
       public DateTime startTime;
       
       
       //dogadjaj svakog buttona
        public void pritisni(Object o, EventArgs e)
        {
           
           textBox1 = new TextBox();
           button2 = new Button();
           button3 = new Button();
          
           Label label1 = new Label();
         
           label1.Location = new System.Drawing.Point(44, 81);
           label1.Name = "label1";
           label1.Size = new System.Drawing.Size(430, 30);
           label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
           label1.Text = n.getIterator().nextPitanje().ToString();
           

           textBox1.Location = new System.Drawing.Point(165, 123);
           textBox1.Name = "textBox1";
           textBox1.Size = new System.Drawing.Size(200, 32);
           textBox1.TabIndex = 0;
           textBox1.CharacterCasing = CharacterCasing.Upper;
           textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyUp);
           textBox1.Select();
           
           button2.Location = new System.Drawing.Point(280, 180);
           button2.Name = "button1";
           button2.Size = new System.Drawing.Size(90, 32);
           button2.TabIndex = 1;
           button2.Text = "Potvrdi";
           button2.UseVisualStyleBackColor = true;
           button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
           button2.Click += new System.EventHandler(button2_Click);

           button3.Location = new System.Drawing.Point(394, 180);
           button3.Name = "button2";
           button3.Size = new System.Drawing.Size(90, 32);
           button3.TabIndex = 1;
           button3.Text = "Otkazi";
           button3.UseVisualStyleBackColor = true;
           button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
           button3.Click += new System.EventHandler(button3_Click);
           f = new PostavkaPitanja();
           f.Show();
           f.Controls.Add(textBox1);
           f.Controls.Add(button2);
           f.Controls.Add(button3);
           f.Controls.Add(label1);
           f.ActiveControl = textBox1;

        
        }
        //za enter
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (textBox1.Text == n.getIterator().nextOdgovor().ToString())
                {
                    //MessageBox.Show("Bravo!");
                    igrica.setState(true, listaButtona);
                    igrica.doAction();
                    f.Close();
                    bodovi += 10;
                    toolStripLabel3_bodovi.Text = bodovi.ToString();

                }
                else
                {
                    igrica.setState(false, listaButtona);
                    igrica.doAction();
                    f.Close();
                }
            }
        }

        public IgricaStanje igrica = new IgricaStanje();
        public IgricaStanjeVisible iV = new IgricaStanjeVisible();
        public IgricaStanjeDisabled iX = new IgricaStanjeDisabled();

        //button za potvrdu odgovora
        public int bodovi=0;
        public void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == n.getIterator().nextOdgovor().ToString())
            {
                igrica.setState(true, listaButtona);
                igrica.doAction();
                f.Close();
                bodovi += 10;
                toolStripLabel3_bodovi.Text = bodovi.ToString();

            }
            else
            {
                igrica.setState(false, listaButtona);
                igrica.doAction();
                f.Close();
            }
        }

        //otkazi button
        public void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == n.getIterator().nextOdgovor().ToString() || textBox1.Text != n.getIterator().nextOdgovor().ToString())
            {

                igrica.setState(false, listaButtona);
                igrica.doAction();
                f.Close();
            }
        }


        public Pogadjanje(TextBox textbox_nick)
        {
            InitializeComponent();
            textBox_nick = textbox_nick;
            this.MinimumSize = new Size(510, 365);
            this.MaximumSize = new Size(510, 365);
            timer1.Start();
            startTime = DateTime.Now;
            
            
            for (int i = 0; i < 3; i++)
            {
                int x = (5 + i * 100);
                for (int j = 0; j <5 ; j++)
                {
                    int y = (5 + j * 95);
                    b = new Button();
                    listaButtona.Add(b);
                    b.Location = new Point(y, x);
                    b.Size = new Size(100, 100);
                    //this.Controls.Add(b);
                    b.Click += new EventHandler(pritisni);

                }
            }

            for (int i = 0; i < listaButtona.Count(); i++)
            {
                this.Controls.Add(listaButtona[i]);
            }
        }

        public Pogadjanje()
        {
            // TODO: Complete member initialization
        }



        //za odbrojavanje
        private void timer1_Tick_1(object sender, EventArgs e)
        {
            int elapsedminutes = (int)(DateTime.Now - startTime).TotalMinutes;
            int remainingminutes = minutesToWait - elapsedminutes;

            if (remainingminutes-- <= 0)
            {
                // run your function
                timer1.Stop();
                
                
                Zavrsna f4 = new Zavrsna(toolStripLabel3_bodovi,textBox_nick);
                f4.Show();
                this.Close();
                f.Close();
                
            }
            
                

             toolStripStatusLabel1.Text=
                String.Format("{0} minuta preostalo...", remainingminutes);
            
        }

       

        private void button1_odgovor_Click(object sender, EventArgs e)
        {
            bool da = false, x=false;
            int brojac = 0;
            for (int i = 0; i < listaButtona.Count(); i++)
            {
                if ((listaButtona[i].Visible == false) || (listaButtona[i].Enabled == false))
                {
                    brojac++;
                    da = true;
                }
                else x = true;
                
            }
            if (((textBox2.Text == "KRAVICE") && da == true) || ((textBox2.Text == "KRAVICE") && x == true))
            {
                int a = 200;
                a -= brojac * 10;
                toolStripLabel3_bodovi.Text = a.ToString();
                timer1.Stop();
                Pobjednicka f = new Pobjednicka(toolStripLabel3_bodovi, textBox_nick);
                f.Show();
                this.Close();

            }
            else 
            {
                MessageBox.Show("Netacno!", "Netacan odgovor",MessageBoxButtons.OK);
                textBox2.Text = "";
            }
        }

        private void textBox2_TextChanged_1(object sender, EventArgs e)
        {
            textBox2.CharacterCasing = CharacterCasing.Upper; 
        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && textBox2.Text == "KRAVICE")
            {
                bool da = false, x = false;
                int brojac = 0;
                for (int i = 0; i < listaButtona.Count(); i++)
                {
                    if ((listaButtona[i].Visible == false) || (listaButtona[i].Enabled == false))
                    {
                        brojac++;
                        da = true;
                    }
                    else x = true;

                }
                if (((textBox2.Text == "KRAVICE") && da == true) || ((textBox2.Text == "KRAVICE") && x == true))
                {
                    int a = 200;
                    a -= brojac * 10;
                    toolStripLabel3_bodovi.Text = a.ToString();
                    timer1.Stop();
                    Pobjednicka f = new Pobjednicka(toolStripLabel3_bodovi, textBox_nick);
                    f.Show();
                    this.Close();

                }
            }
            else if(e.KeyCode == Keys.Enter && textBox2.Text != "KRAVICE")
            {
                MessageBox.Show("Netacno!", "Netacan odgovor", MessageBoxButtons.OK);
                textBox2.Text = "";
            }
               
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
            Pocetna p = new Pocetna();
            p.Show();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.Close();
            Pocetna p = new Pocetna();
            p.Show();
        }

      
           
        
          
            
            

    }
}
